package com.qiuzhiyi.fragment;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.li.utils.KeyboardUtil;
import com.qiuzhiyi.R;
import com.qiuzhiyi.activity.LoginActivity;
import com.qiuzhiyi.base.QZYBaseFragment;
import com.qiuzhiyi.cache.HumanCache;
import com.qiuzhiyi.net.ObservableHelper;
import com.qiuzhiyi.net.RetrofitHelper;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Author: Li
 * Date: 2017/11/13 0013
 * Description: "返费.补贴"
 */

public class AllowanceFragment extends QZYBaseFragment {


    @BindView(R.id.tab_layout)
    TabLayout mTabLayout;
    @BindView(R.id.pager)
    ViewPager mPager;

    private List<String> mTitleList;
    private List<Fragment> mFragmentList;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_allowance;
    }

    public static AllowanceFragment newInstance() {

        Bundle bundle = new Bundle();

        AllowanceFragment fragment = new AllowanceFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void initSth() {
        super.initSth();

    }


    @Override
    protected void onFirstVisible() {
        super.onFirstVisible();
        mFragmentList = new ArrayList<>();
        mFragmentList.add(AllowanceListFragment.newInstance("1"));
        mFragmentList.add(AllowanceListFragment.newInstance("2"));
        mFragmentList.add(AllowanceListFragment.newInstance("3"));
        mTitleList = Arrays.asList("还费榜", "推荐榜","出名单");
        mPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragmentList.get(position);
            }

            @Override
            public int getCount() {
                return mFragmentList.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return mTitleList.get(position);
            }
        });
        mTabLayout.setupWithViewPager(mPager);
    }


    @OnClick(R.id.txt_recommend)
    public void onViewClicked() {
        mItem = HumanCache.getHumanInfo(mContext);
        if (mItem == null) {
            mUid="";
        }else {
            mUid=String.valueOf(mItem.getHumanId());
        }
        if (TextUtils.isEmpty(mUid)) {
            start(LoginActivity.class);
            return;
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_recommend, null);
        builder.setView(view);
        final AlertDialog dialog = builder.create();
        if (dialog.getWindow() != null) {
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }
        dialog.show();

        final EditText etName = view.findViewById(R.id.et_name);
        final EditText etPhone = view.findViewById(R.id.et_phone);
        TextView txtClose=view.findViewById(R.id.txt_close);
        TextView txtConfirm=view.findViewById(R.id.txt_confirm);

        txtClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        txtConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                KeyboardUtil.hideKeyboard(mContext,v);
                if (isEmpty(etName)||isEmpty(etPhone)) {
                    showToast("请输入完整信息");
                    return;
                }
                dialog.dismiss();
                recommend(etName.getText().toString(),etPhone.getText().toString());
            }
        });
    }
    private void recommend(String name, String phone) {
        showLoadingDialog("正在推荐");
        Map<String, String> map = new HashMap<>();
        map.put("sms_title", name);
        map.put("sms_content", phone);
        ObservableHelper.createObservable(RetrofitHelper.createService().recommend(map))
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(String s) {
                        dismissLoadingDialog();
                        try {
                            JSONObject object = new JSONObject(s);
                            switch (object.getString("code")) {
                                case "200":
                                    showToast("推荐成功");
                                    break;
                                default:
                                    showToast("推荐失败");
                                    break;
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissLoadingDialog();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
